<template>
  <div class="battery-distribution-chart">
    <div v-loading="loading" class="chart-container">
      <div ref="chartRef" class="chart"></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted, watch, nextTick } from 'vue'
import * as echarts from 'echarts'

interface BatteryDistributionData {
  range: string
  count: number
}

interface Props {
  data: BatteryDistributionData[]
  loading?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  loading: false
})

const chartRef = ref<HTMLElement>()
let chartInstance: echarts.ECharts | null = null

const initChart = () => {
  if (!chartRef.value) return
  
  chartInstance = echarts.init(chartRef.value)
  updateChart()
}

const updateChart = () => {
  if (!chartInstance || !props.data?.length) return
  
  const ranges = props.data.map(item => item.range)
  const counts = props.data.map(item => item.count)
  
  // 根据电量范围设置颜色
  const getBarColor = (range: string) => {
    if (range.includes('90') || range.includes('100')) return '#67C23A' // 绿色
    if (range.includes('70') || range.includes('80')) return '#90EE90' // 浅绿色
    if (range.includes('50') || range.includes('60')) return '#FFD700' // 金色
    if (range.includes('30') || range.includes('40')) return '#FFA500' // 橙色
    if (range.includes('10') || range.includes('20')) return '#FF4500' // 橙红色
    return '#F56C6C' // 红色
  }
  
  const colors = ranges.map(range => getBarColor(range))
  
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      },
      formatter: (params: any) => {
        const param = params[0]
        return `${param.name}<br/>设备数量: <strong>${param.value}</strong>`
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: ranges,
      axisTick: {
        alignWithLabel: true
      },
      axisLabel: {
        fontSize: 11
      }
    },
    yAxis: {
      type: 'value',
      name: '设备数量',
      nameLocation: 'middle',
      nameGap: 30
    },
    series: [
      {
        name: '设备数量',
        type: 'bar',
        barWidth: '60%',
        data: counts,
        itemStyle: {
          color: (params: any) => colors[params.dataIndex]
        },
        label: {
          show: true,
          position: 'top',
          formatter: '{c}'
        }
      }
    ]
  }
  
  chartInstance.setOption(option, true)
}

const resizeChart = () => {
  if (chartInstance) {
    chartInstance.resize()
  }
}

// 监听数据变化
watch(() => props.data, () => {
  nextTick(() => {
    updateChart()
  })
}, { deep: true })

// 监听加载状态
watch(() => props.loading, (loading) => {
  if (!loading) {
    nextTick(() => {
      updateChart()
    })
  }
})

onMounted(() => {
  nextTick(() => {
    initChart()
  })
  
  window.addEventListener('resize', resizeChart)
})

onUnmounted(() => {
  if (chartInstance) {
    chartInstance.dispose()
  }
  window.removeEventListener('resize', resizeChart)
})
</script>

<style scoped lang="scss">
.battery-distribution-chart {
  .chart-container {
    position: relative;
    
    .chart {
      width: 100%;
      height: 250px;
    }
  }
}
</style>